package com.macro.mall.controller.oms;

/**
 * class
 *
 * @author xuyanjun
 * @date 2020/1/15
 */

import com.github.pagehelper.Page;
import com.macro.mall.commission.enums.OrderSettleArrivedStatusEnum;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.oms.OrderCancelReqDto;
import com.macro.mall.dto.oms.OrderDeliveryReqDto;
import com.macro.mall.dto.oms.PagedQueryOrderReqDto;
import com.macro.mall.dto.ums.PagedQueryMemberReqDto;
import com.macro.mall.oms.domain.Order;
import com.macro.mall.oms.entity.OrderEntity;
import com.macro.mall.oms.service.order.OrderService;
import com.macro.mall.oms.service.order.bo.PagedQueryOrderInput;
import com.macro.mall.oms.service.order.bo.UpdateOrderInput;
import com.macro.mall.ums.domain.Member;
import com.macro.mall.ums.entity.MemberEntity;
import com.macro.mall.ums.service.account.bo.PagedQueryMemberInput;
import com.macro.mall.util.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 订单信息控制器
 *
 * @author xuyanjun
 * @date 2019-12-22
 */
@Api(tags = "订单信息")
@Slf4j(topic = "OrderController")
@RestController
@RequestMapping("/order/v2")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("订单取消")
    @PostMapping("/cancel")
    public CommonResult cancel(@RequestBody OrderCancelReqDto reqDto) {
        final OrderEntity orderEntity = orderService.getById(reqDto.getOrderId());
        Assert.isTrue(1 == orderEntity.getStatus(), "只有处于待发货的订单才能发货。");

        String notifyUrl = WebUtils.getRequestDomain() + "refund/callback/weChat/v1";

        orderService.cancel(reqDto.getOrderId(),notifyUrl);

        return CommonResult.success(null);
    }

    @ApiOperation("订单发货")
    @PostMapping("/delivery")
    public CommonResult delivery(@RequestBody OrderDeliveryReqDto reqDto) {
        final OrderEntity orderEntity = orderService.getById(reqDto.getOrderId());
        Assert.isTrue(1 == orderEntity.getStatus(), "只有处于待发货的订单才能发货。");

        UpdateOrderInput updateOrderInput = new UpdateOrderInput();
        updateOrderInput.setId(orderEntity.getId())
                .setStatus(2)
                .setDeliveryCompany(reqDto.getDeliveryCompany())
                .setDeliverySn(reqDto.getDeliverySn());

        orderService.update(updateOrderInput);

        return CommonResult.success(null);
    }

    /**
     * 分页查询订单
     *
     * @return 分页后的会员
     */
    @ApiOperation("分页查询订单")
    @PostMapping("pagedQuery")
    public CommonPage<Order> pagedQuery(@RequestBody PagedQueryOrderReqDto reqDto) {
        PagedQueryOrderInput pagedQueryOrderInput = new PagedQueryOrderInput();
        pagedQueryOrderInput
                .setMemberId(reqDto.getUserId())
                .setPageIndex(reqDto.getPageIndex())
                .setPageSize(reqDto.getPageSize());

        Page<OrderEntity> page = orderService.pagedQuery(pagedQueryOrderInput);
        List<Order> orders = orderService.entityToDomain(page);

        return CommonPage.restPage(orders,page);
    }
}
